﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.ComponentModel;
using System.Data.SqlClient;
using System.Configuration;
using System.Data;


namespace WebApplication10
{
     [DataObject]
    public class Resume
    {
        [DataObjectField(true, true)]
        public int ResumeID { get; set; }
        public string ResumeTitle { get; set; }
        public string Name { get; set; }
        public string Address { get; set; }
        public string Education { get; set; }
        public string Experience { get; set; }
        public string Skills { get; set; }
        public string imagePath { get; set; }
        public string imageName { get; set; }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static Resume GetResumeByID(string ResumeID)
        {
            SqlConnection conn = new SqlConnection();
            Resume  R = null;

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "select * from Resume where ResumeID=@ResumeID ";
                comm.Parameters.AddWithValue("@ResumeID", ResumeID);
                SqlDataReader dr = comm.ExecuteReader();
                while (dr.Read())
                {
                    R = new Resume();
                    R.ResumeID = (int)dr["ResumeID"];
                    R.ResumeTitle =(string)dr["ResumeTitle"];
                    R.Name = (string)dr["Name"];
                    R.Address  = (string)dr["Address"];
                    R.Education  = (string)dr["Education"];
                    R.Experience  = (string)dr["Experience"];
                    R.Skills = (string)dr["Skills"];
                    R.imageName = (string)dr["imageName"];
                    R.imagePath = (string)dr["imagePath"];
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return R;
        }

        [DataObjectMethod(DataObjectMethodType.Select, true)]
        public static List<Resume> GetResumes()
        {
            List<Resume> results = new List<Resume>();
            SqlConnection conn = null;

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Select * from Resume";
                SqlDataReader dr = comm.ExecuteReader();

                while (dr.Read())
                {
                    Resume R = new Resume();
                    R.ResumeID = (int)dr["ResumeID"];
                    R.ResumeTitle =(string)dr["ResumeTitle"];
                    R.Name = (string)dr["Name"];
                    R.Address = (string)dr["Address"];
                    R.Education = (string)dr["Education"];
                    R.Experience = (string)dr["Experience"];
                    R.Skills = (string)dr["Skills"];
                    R.imageName = (string)dr["imageName"];
                    R.imagePath = (string)dr["imagePath"];
                }
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return results;
        }

        [DataObjectMethod(DataObjectMethodType.Update, true)]
        public static void UpdateResume(Resume R)
        {
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText =
                    "Update Resume set =@ResumeTitle, Name=@Name, Address=@Address,Education=@Education, Experience=@Experience,imagePath =@imagePath,imageName=@imageName Skills=@Skills where ResumeID=@ResumeID";

                comm.Parameters.AddWithValue("@ResumeTitle", R.ResumeTitle);
                comm.Parameters.AddWithValue("@Name", R.Name);
                comm.Parameters.AddWithValue("@Address", R.Address);
                comm.Parameters.AddWithValue("@Education", R.Education);
                comm.Parameters.AddWithValue("@Experience", R.Experience);
                comm.Parameters.AddWithValue("@ResumeID", R.ResumeID);
                comm.Parameters.AddWithValue("@imagePath", R.imagePath);
                comm.Parameters.AddWithValue("@imageName", R.imageName);
                comm.Parameters.AddWithValue("@Skills", R.Skills);
                int rowsAffected = comm.ExecuteNonQuery();

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        [DataObjectMethod(DataObjectMethodType.Delete, true)]
        public static void DeleteResume(Resume R)
        {
            SqlConnection conn = new SqlConnection();

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText =
                    "Delete from Resume WHERE ResumeID=@ResumeID";
                comm.Parameters.AddWithValue("@ResumeID", R.ResumeID);
                int rowsAffected = comm.ExecuteNonQuery();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
        }
        [DataObjectMethod(DataObjectMethodType.Insert, true)]
        public static int InsertResume(Resume R)
        {
            SqlConnection conn = null;
            int newResumeID = 0;

            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                comm.Connection = conn;
                comm.CommandText = "Insert into Resume ( ResumeTitle, Name,Address,Education,Experience,imagePath,imageName,Skills) VALUES ( @ResumeTitle, @Name,@Address,@Education,@Experience,@imagePath,@imageName,@Skills)";

                comm.Parameters.AddWithValue("@ResumeTitle", R.ResumeTitle);
                comm.Parameters.AddWithValue("@Name", R.Name);
                comm.Parameters.AddWithValue("@Address", R.Address);
                comm.Parameters.AddWithValue("@Education", R.Education);
                comm.Parameters.AddWithValue("@Experience", R.Experience);
      
                comm.Parameters.AddWithValue("@imagePath", R.imagePath);
                comm.Parameters.AddWithValue("@imageName", R.imageName);
                comm.Parameters.AddWithValue("@Skills", R.Skills);
                int rowsAffected = comm.ExecuteNonQuery();
                comm.CommandText = "select @@IDENTITY as newResumeID";
                SqlDataReader dr = comm.ExecuteReader();
                dr.Read();
                newResumeID = int.Parse(dr["NewResumeID"].ToString());
                dr.Close();
            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return newResumeID;
        }


        public static int retrieveResume(int ResumeID)
        {
            SqlConnection conn = null;
            int results = 0;
            try
            {
                conn = new SqlConnection();
                string connstr = ConfigurationManager.ConnectionStrings["BlueSearchDBString"].ToString();
                conn.ConnectionString = connstr;
                conn.Open();
                SqlCommand comm = new SqlCommand();
                SqlDataReader dr = comm.ExecuteReader();
                comm.Connection = conn;
                comm.CommandText = "select count(Title) as count from Resume";
                while (dr.Read())
                {
                    results = (int)dr["count"];
                }

            }
            catch (SqlException ex)
            {
                throw ex;
            }
            finally
            {
                conn.Close();
            }
            return results;


        }
    }
 }